<?php

namespace Admin\Controller;



class ManagerController extends BaseController {
    //管理员显示
    public function lst(){
                          
            //设置超级管理员不能删除
            $admin =C('RBAC_SUPERADMIN');
            $this->assign('admin',$admin);
            
            $User = M('User');
            $count = $User->count();
            $Page = new \Think\Page($count,5);
            $show = $Page->show();
            if (I('search')==''){
                $user = $User->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('user',$user);
                $this->assign('page',$show);
            }else {
                $search =trim(I('search'));
                $map['username']=array('like',"%$search%");
                $map['remark'] = array('like',"%$search%");
                $map['_logic'] = 'or';
                $user = $User->where($map)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('user',$user);
                $this->assign('page',$show);
            }
            $this->display();
       
    }
    //管理员添加
    public function ad(){
            

        
            if (IS_POST){
                $User = M('User');
                $data =array(
                        'username'=>I('username'),
                        'password'=>I('password'),
                        'remark'=>I('remark'),
                        'status'=>I('status'),
                        'createtime'=>time(),
                    );
                if ($User->create($data)){
                    //获取新增的id
                    $uid =$User->add();
                    if ($uid){
                        $this->success('新增成功',U('lst'));
                    }else {
                        $this->error('新增失败！');
                    }
                }else {
                    $this->error($User->getError());
                }
                return ;
            }
            
            $this->display();
       
    }

    
    public function edt($id){

        
            $User = M('User');
            $user =$User->find($id);
            $this->assign('user',$user);
            
            if(IS_POST){
                $data = array(
                    'id'=>I('id'),
                    'remark'=>I('remark'),
                    'status'=>I('status'),
                );
                if (I('password') == ''){
                    $data['password']=$user['password'];
                }else {
                    $data['password']=md5(I('password'));
                }
                if ($User->where()->save($data)){
                    $this->success('修改成功',U('lst'));
                }else {
                    $this->error('修改失败！');
                }
              
                return ;
            }
                
            
    
            $this->display();
        
        
        
    } 
    
    
    
    //管理员单删除
    public function del($id){
        
            $User =M('User');
            if($id==1){
                $this->error('此用户不能删除');
                exit();
            }
            if ($User->delete($id)){
                $this->success('删除成功！',U('lst'));
            }else {
                $this->error('删除失败');
            }
        
    }

    
    
    
    

    
    
}